package com.adgox.tiantianbiting.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.adgox.tiantianbiting.R;
import com.adgox.tiantianbiting.bean.ProgramDetailBean;
import com.adgox.tiantianbiting.bean.SubProgramBean;
import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by tang on 2019/3/15.
 */

public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.ViewHolder> implements View.OnClickListener {
    private List<SubProgramBean> datas;

    public RecommendAdapter(List<SubProgramBean> datas) {
        this.datas = datas;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recommend, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        SubProgramBean bean = datas.get(position);
        Glide.with(holder.itemView.getContext()).load(bean.getShareImageUrl()).into(holder.ivBook);
        holder.tvBook.setText(bean.getTitle());
        holder.tvDescribe.setText(bean.getDescribe());
        holder.tvListen.setText(bean.getListenNum()+"");
        holder.tvSeason.setText(bean.getTotalSeason()+"集");
        if (bean.getMarkStatus() == 0){
            holder.tvCollect.setTextColor(Color.parseColor("#e64340"));
            holder.ivCollect.setImageResource(R.drawable.icon_collected);
        } else {
            holder.tvCollect.setTextColor(Color.parseColor("#888888"));
            holder.ivCollect.setImageResource(R.drawable.icon_uncollected);
        }

        holder.ivCollect.setTag(position);
        holder.tvCollect.setTag(position);
        holder.itemView.setTag(position);

        holder.ivCollect.setOnClickListener(this);
        holder.tvCollect.setOnClickListener(this);
        holder.itemView.setOnClickListener(this);
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    public void addDatas(List<SubProgramBean> list){
        datas.addAll(list);
    }

    public void clear(){
        datas.clear();
        notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        switch (v.getId()){
            case R.id.iv_collect:
            case R.id.tv_collect:
                listener.onItemClick(v,ViewName.COLLECT,position);
                break;
            default:
                listener.onItemClick(v,ViewName.ITEM,position);
                break;
        }
    }

    public enum ViewName{
        COLLECT,
        ITEM
    }

    public interface onItemClickListener{
        void onItemClick(View view,ViewName viewName,int position);
    }

    public onItemClickListener listener = null;

    public void setOnItemClickListener(onItemClickListener listener){
        this.listener = listener;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.iv_book)
        ImageView ivBook;
        @BindView(R.id.tv_book)
        TextView tvBook;
        @BindView(R.id.iv_collect)
        ImageView ivCollect;
        @BindView(R.id.tv_collect)
        TextView tvCollect;
        @BindView(R.id.tv_describe)
        TextView tvDescribe;
        @BindView(R.id.tv_listen)
        TextView tvListen;
        @BindView(R.id.tv_season)
        TextView tvSeason;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
